package com.oms.config;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author zhanghailong
 * @Description TODO
 * @Date 16:03 2020/3/26
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    /**
     * 分组名称
     */
    @Value("${swagger.apiGroupName}")
    private String apiGroupName;

    /**
     * 文档标题
     */
    @Value("${swagger.title}")
    private String title;

    /**
     * 文档描述
     */
    @Value("${swagger.description}")
    private String description;

    /**
     * api版本
     */
    @Value("${swagger.version}")
    private String version;

    /**
     * service url
     */
    @Value("${swagger.serviceUrl}")
    private String serviceUrl;

    /**
     * controller 包路径
     */
    @Value("${swagger.packAge}")
    private String packAge;

    @Bean
    public Docket serviceApi() {
        // 请求类型过滤规则
        Predicate<RequestHandler> controllerPredicate = getControllerPredicate();
        // controller 包路径
        Predicate<RequestHandler> controllerPackage = RequestHandlerSelectors.basePackage(packAge);
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();
        tokenPar.name("_csrf").description("csrf").modelRef(new ModelRef("string")).parameterType("query").required(false).build();
        pars.add(tokenPar.build());

        return new Docket( DocumentationType.SWAGGER_2 )
                .groupName( apiGroupName )
                .forCodeGeneration( true )
                .select()
                .apis( controllerPackage )
                .apis( controllerPredicate )
                .build()
                .apiInfo( serviceApiInfo() )
                .globalOperationParameters( pars );
    }

    /**
     * ServiceApi 描述信息
     * @return
     */
    private ApiInfo serviceApiInfo() {
        // 大标题
        return new ApiInfoBuilder()
                .title(title)
                // 详细描述
                .description(description)
                // 版本
                .version(version)
                .termsOfServiceUrl(serviceUrl)
                .build();
    }

    /**
     * 定义swagger 对请求类型的过滤规则 只显示ajax请求的Controller
     * @return
     */
    private Predicate<RequestHandler> getControllerPredicate() {
        return Predicates.or(
                RequestHandlerSelectors.withClassAnnotation( RestController.class)
                ,RequestHandlerSelectors.withMethodAnnotation( ResponseBody.class)
//                添加自定义api
//                ,RequestHandlerSelectors.withMethodAnnotation(SwaggerShowApi.class)
        );
    }

}
